package com.le.sarrs.util;

import net.hexi.sword.date.DateTimes;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private final static Logger log = LoggerFactory.getLogger(DateUtil.class);

    /***
     * 根据传入的时间(时间戳格式)返回前端显示格式时间
     *
     * @param time milliseconds
     * @return time
     * @author gaojiawei
     */
    public static String getDateStrForShow(long time) {
        String dateShow = "";
        Calendar calNow = Calendar.getInstance();
        calNow.setTime(new Date());
        long calNowTime = calNow.getTimeInMillis();

        long minutes = (calNowTime - time) / (60 * 1000);


        if (minutes >= 365 * 2 * 24 * 60) {
            dateShow = minutes / 365 / 24 / 60 + " years ago";
        } else if (365 * 24 * 60 <= minutes && minutes < 365 * 2 * 24 * 60) {
            dateShow = "1 year ago";
        } else if (30 * 24 * 60 <= minutes && minutes < 365 * 24 * 60) {
            long xMonth = minutes / 30 / 24 / 60;
            if (xMonth == 1) {
                dateShow = xMonth + " month ago";
            } else {
                dateShow = xMonth + " months ago";
            }
        } else if (7 * 24 * 60 <= minutes && minutes < 30 * 24 * 60) {
            long xweeks = minutes / 7 / 24 / 60;
            if (xweeks == 1) {
                dateShow = xweeks + " week ago";
            } else {
                dateShow = xweeks + " weeks ago";
            }
        } else if (24 * 60 <= minutes && minutes < 7 * 24 * 60) {
            long xdays = minutes / 24 / 60;
            if (xdays == 1) {
                dateShow = "1 day ago";
            } else {
                dateShow = xdays + " days ago";
            }
        } else if (60 <= minutes && minutes < 24 * 60) {
            long xminutes = minutes / 60;
            if (1 == xminutes) {
                dateShow = xminutes + " hour ago";
            } else {
                dateShow = xminutes + " hours ago";
            }
        } else if (1 <= minutes && minutes < 60) {

            if (1 == minutes) {
                dateShow = minutes + " minute ago";
            } else {
                dateShow = minutes + " minutes ago";
            }
        }

        log.info("theTime=" + time + "|" + "Now=" + calNowTime + "|minutes=" + minutes + "|dateShow=" + dateShow);
        return dateShow;
    }


    /**
     * get the time by offset to current time
     *
     * @param type (hour,day,month...)
     * @param add  (offset for current time)
     * @return time
     */
    public static Date getIndexDayRefCurr(int type, int add) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(type, add);
        return calendar.getTime();
    }


    /**
     * 获得当前时间，格式yyyy-MM-dd hh:mm:ss
     *
     * @return time
     */
    public static String getCurrentDate() {
        return getCurrentDate("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获得当前时间，格式自定义
     *
     * @param format 自定义
     * @return time
     */
    public static String getCurrentDate(String format) {
        Calendar day = Calendar.getInstance();
        day.add(Calendar.DATE, 0);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(day.getTime());
    }

    /**
     * 获得指定格式时间字符串
     *
     * @param format yyyy...
     * @return time
     */
    public static String getFormatDate(long time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    /**
     * 获得指定格式时间字符串
     *
     * @param format yyyy...
     * @return time
     */
    public static String getFormatDate(String time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        DateTime dateTime = DateTimes.parse(time);
        return sdf.format(new Date(dateTime.getMillis()));
    }

}